﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using CE.Global;
using CE.Database;

public partial class car_insurance_ggquick : MyLanguageAndSessionBasePage
{
    int nowYear = int.Parse(General.GetUTCDateTime().Year.ToString());
    protected void Page_Load(object sender, EventArgs e)
    {
        //if (!Page.IsPostBack)
        //{
        //    BindRptCarMake();
        //    if (Session["SCarModelCC"] != null && Session["SCarModelCC"].ToString() != "")
        //    {
        //        BindRptCarModel(Session["SCarMake"].ToString());
        //        hidCarMake.Value = Session["SCarMake"].ToString();
        //        hidCarMakeFull.Value = Session["SCarMakeFull"].ToString();
        //        lbnSelCarMake.Text = Resources.CarInsurance.CarMake + " " + Session["SCarMakeFull"].ToString();
        //        lbnSelCarModel_Click(sender, e);
        //    }
        //}
    }


    #region CarMake
    private void BindRptCarMake()
    {

        //MyDatabase _db = new MyDatabase();
        //DataTable _dt = new DataTable("tblMake");
        //SortedList _spList = new SortedList();
        //_spList.Add("@Year", carYear);
        //_dt = _db.GetDataTableBySP("sprocGetCarMakeTHListByYear", _spList);

        //if (_dt != null && _dt.Rows.Count > 0)
        //{
        //    rptCarMake.DataSource = _dt;
        //    rptCarMake.DataBind();
        //}

        //_dt = null;
        //_db = null;

        DataTable _dt = new DataTable("tblCarMake");
        DataRow _dr;
        _dt.Columns.Add("MakeDesc");
        _dt.Columns.Add("MakeCode");
        string[,] arrCarMake = new string[,] 
                { { "Chevrolet","CHEV" }, 
                { "Ford", "FORD" }, 
                { "Honda", "HOND" }, 
                { "Isuzu", "ISUZ" }, 
                { "Mazda", "MAZD" }, 
                { "Mitsubishi", "MITS" }, 
                { "Nissan", "NISS" }, 
                { "Toyota", "TOYO" } };
        for (int i = 0; i < arrCarMake.Length / 2; i++)
        {
            _dr = _dt.NewRow();

            _dr["MakeDesc"] = arrCarMake[i, 0];
            _dr["MakeCode"] = arrCarMake[i, 1];
            _dt.Rows.Add(_dr);
        }

        rptCarMake.DataSource = _dt;
        rptCarMake.DataBind();
        _dt = null;
    }

    protected void rptCarMake_ItemCommand(object sender, RepeaterCommandEventArgs e)
    {
        LinkButton lbnCarmake = (LinkButton)e.Item.FindControl("lbnCarmake");
        Label lblMakeCode = (Label)e.Item.FindControl("lblMakeCode");
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
        }

        if (e.CommandName.ToLower().Equals("carmake"))
        {
            hidCarMake.Value = lblMakeCode.Text;
            hidCarMakeFull.Value = lbnCarmake.Text;
            BindRptCarModel(lblMakeCode.Text);
            lbnSelCarMake.Text = Resources.CarInsurance.CarMake+" " + lbnCarmake.Text;
            divCarYear.Style.Add("display", "none");
            divCarMake.Style.Add("display", "none");
            divCarModel.Style.Add("display", "");
            lblChooseModel.Text = Resources.CarInsurance.CarModelDetail.Replace("@$car$@", lbnCarmake.Text);
        }
    }

    protected void rptCarMake_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            HyperLink hplCarMake = (HyperLink)e.Item.FindControl("hplCarMake");
            Label lblMakeCode = (Label)e.Item.FindControl("lblMakeCode");
            //hplCarMake.NavigateUrl = "~/car-insurance/Class1CarModel.aspx" + "?carmake=" + lblMakeCode.Text;
            
        }
    }
    #endregion

    #region CarModel
    private void BindRptCarModel(string makeCode)
    {
        MyDatabase _db = new MyDatabase();
        DataTable _dt = new DataTable("tblCarModel");
        SortedList _spList = new SortedList();
        _spList.Add("@CarMake", makeCode);
        _spList.Add("@Year", nowYear - 9);
        _dt = _db.GetDataTableBySP("sprocGetCarModelDescTH10YearListByCarMakeCode", _spList);

        if (_dt != null && _dt.Rows.Count > 0)
        {
            rptCarModel.DataSource = _dt;
            rptCarModel.DataBind();
        }

        _dt = null;
        _db = null;
    }
    protected void rptCarModel_ItemCommand(object sender, RepeaterCommandEventArgs e)
    {
        LinkButton lbnCarModel = (LinkButton)e.Item.FindControl("lbnCarModel");
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
        }
        if (e.CommandName.ToLower().Equals("carmodel"))
        {

            hidCarModel.Value = lbnCarModel.Text;
            BindRptCarYear();
            lbnSelCarModel.Text = Resources.CarInsurance.CarModel + " " + lbnCarModel.Text;
            divCarMake.Style.Add("display", "none");
            divCarModel.Style.Add("display", "none");
            divCarYear.Style.Add("display", "");

        }
    }

    protected void rptCarModel_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            HyperLink hplCarModel = (HyperLink)e.Item.FindControl("hplCarModel");
            Label lblCarModel = (Label)e.Item.FindControl("lblCarModel");
            //hplCarModel.NavigateUrl = "~/car-insurance/Class1CarYear.aspx" + "?carmake=" + Request.QueryString["carmake"].ToString() + "&carmodel=" + lblCarModel.Text;
        }
    }
    #endregion

    #region CarYear
    private void BindRptCarYear()
    {
        string[] _tempDesc = hidCarModel.Value.Split(' ');
        string CarModelName = "";
        string Body="";
        int CC=0;
        int Door=0;
        if (_tempDesc.Length == 5)
        {
            CarModelName = _tempDesc[0];
            Body = _tempDesc[1];
            CC = Convert.ToInt32(_tempDesc[2].Substring(0, _tempDesc[2].Length - 2));
            Door = Convert.ToInt32(_tempDesc[3]);
        }
        else if (_tempDesc.Length == 6)
        {
            CarModelName = _tempDesc[0] + " " + _tempDesc[1];
            Body = _tempDesc[2];
            CC = Convert.ToInt32(_tempDesc[3].Substring(0, _tempDesc[3].Length - 2));
            Door = Convert.ToInt32(_tempDesc[4]);
        }
        else if (_tempDesc.Length == 7)
        {
            CarModelName = _tempDesc[0] + " " + _tempDesc[1] + " " + _tempDesc[2];
            Body = _tempDesc[3];
            CC = Convert.ToInt32(_tempDesc[4].Substring(0, _tempDesc[4].Length - 2));
            Door = Convert.ToInt32(_tempDesc[5]);
        }
        MyDatabase _db = new MyDatabase();
        DataTable _dt = new DataTable("tblCarYear");
        SortedList _spList = new SortedList();
        _spList.Add("@CarMake", hidCarMake.Value);
        _spList.Add("@CarModel", CarModelName);
        _spList.Add("@CC", CC);
        _spList.Add("@Door", Door);
        _dt = _db.GetDataTableBySP("sprocGetCarYearByMakeAndModel", _spList);

        if (_dt != null && _dt.Rows.Count > 0)
        {
            rptCarYear.DataSource = _dt;
            rptCarYear.DataBind();
        }

        _dt = null;
        _db = null;


        //DataTable _dt = new DataTable("tblCarYear");
        //DataRow _dr;
        //_dt.Columns.Add("carYear");
        //_dt.Columns.Add("carYearShow");

        ////int nowYear = int.Parse(General.GetUTCDateTime().Year.ToString());
        //for (int i = 0; i < 10; i++)
        //{
        //    _dr = _dt.NewRow();

        //    _dr["carYear"] = nowYear.ToString();
        //    _dr["carYearShow"] = nowYear.ToString(); //check language
        //    _dt.Rows.Add(_dr);
        //    nowYear = nowYear - 1;
        //}

        //rptCarYear.DataSource = _dt;
        //rptCarYear.DataBind();
        //_dt = null;
    }

    protected void rptCarYear_ItemCommand(object sender, RepeaterCommandEventArgs e)
    {
        LinkButton lbnCarYear = (LinkButton)e.Item.FindControl("lbnCarYear");
        Label lblCarYear = (Label)e.Item.FindControl("lblCarYear");
        if (e.CommandName.ToLower().Equals("caryear"))
        {
            Session["SCarYear"] = lbnCarYear.Text;
            Session["SCarMake"] = hidCarMake.Value;
            Session["SCarMakeFull"] = hidCarMakeFull.Value;
            Session["SCarModelCC"] = hidCarModel.Value;
            Response.Redirect(CE.Global.General.GetUrl("/car-insurance/Class1Person.aspx", Page));
            //hidCarYear.Value = lblCarYear.Text;
            //BindRptCarMake(lblCarYear.Text);
            //lbnSelCarYear.Text = " Manufacture Year : " + lbnCarYear.Text;
            //divCarYear.Style.Add("display", "none");
            //divCarMake.Style.Add("display", "");
            //divCarModel.Style.Add("display", "none");

        }
    }

    protected void rptCarYear_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            HyperLink hplCarYear = (HyperLink)e.Item.FindControl("hplCarYear");
            Label lblCarYear = (Label)e.Item.FindControl("lblCarYear");
            //hplCarYear.NavigateUrl = "~/car-insurance/Class1Person.aspx" + "?caryear=" + lblCarYear.Text;
        }
    }
    #endregion
   
    #region StepBack
    protected void lbnSelCarMake_Click(object sender, EventArgs e)
    {
        divCarMake.Style.Add("display", "");
        divCarModel.Style.Add("display", "none");
        divCarYear.Style.Add("display", "none");
        hidCarMake.Value = "";
        hidCarModel.Value = "";
        lbnSelCarMake.Text = "";
        lbnSelCarModel.Text = "";

    }
    protected void lbnSelCarModel_Click(object sender, EventArgs e)
    {
        divCarMake.Style.Add("display", "none");
        divCarModel.Style.Add("display", "");
        divCarYear.Style.Add("display", "none");
        hidCarModel.Value = "";
        lbnSelCarModel.Text = "";
    }
    #endregion
}
